import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/ShowRequest")
public class ShowRequest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        // 把生成的响应的 body 给放到 respBody 中
        StringBuilder respBody = new StringBuilder();
        respBody.append(req.getProtocol());//获取协议类型
        respBody.append("<br>");
        respBody.append(req.getMethod());//获取方法类型
        respBody.append("<br>");
        respBody.append(req.getRequestURI());//获取URL
        respBody.append("<br>");
        respBody.append(req.getContextPath());//获取上下文
        respBody.append("<br>");
        respBody.append(req.getQueryString());//获取查询字符串
        respBody.append("<br>");

        respBody.append("<h3>headers:</h3>");
        Enumeration<String> headerNames = req.getHeaderNames();//获取header中的各种类型信息
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            respBody.append(headerName + ": ");
            respBody.append(req.getHeader(headerName));
            respBody.append("<br>");
        }
        resp.getWriter().write(respBody.toString());//以字符串的形式写入

    }

    //    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //super.doGet(req, resp);
//        StringBuffer str=new StringBuffer();
//
//        str.append(req.getMethod()+" "+req.getProtocol());
//        str.append("<br>");
//        str.append(req.getRequestURI());
//        str.append("<br>");
//        str.append(req.getContextPath());
//        str.append("<br>");
//        str.append(req.getQueryString());
//        str.append("<br>");
//        str.append(req.getParameterNames().toString());
//        str.append("<br>");
//        //str.append(req.getParameter());
//        str.append(req.getHeaderNames().toString());
//        str.append("<br>");
//        str.append(req.getCharacterEncoding()+" "+req.getContentType());
//        str.append("<br>");
//        resp.setContentType("text/html;charset=utf8");
//        resp.getWriter().write(str.toString());
//    }
}
